package com.interrobang.gui;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.interrobang.anime.FileImporter;


public class MenuPanel extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 7654903022614798823L;
	JButton newGame;
	JButton addAnime;
	JButton view;
	JButton importer;
	MainPanel panel;
	JLabel title;
	ButtonListener listener = new ButtonListener();
	
        public MenuPanel()
        {
            
        }
	public MenuPanel(MainPanel panel)
	{
		this.panel = panel;
		initComponents();		
	}
	
	 private void initComponents() {

	        title = new javax.swing.JLabel();
	        newGame = new javax.swing.JButton();
	        addAnime = new javax.swing.JButton();
	        view = new javax.swing.JButton();
	        importer = new javax.swing.JButton();

	        title.setFont(new java.awt.Font("Comic Sans MS", 0, 36)); // NOI18N
	        title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	        title.setText("The Anime Drinking Game!");

	        newGame.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
	        newGame.setText("New Game");
	        newGame.addActionListener(listener);
	        addAnime.addActionListener(listener);
	        view.addActionListener(listener);
	        importer.addActionListener(listener);

	        addAnime.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
	        addAnime.setText("Add a new Anime");

	        view.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
	        view.setText("View Animes");

	        importer.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
	        importer.setText("import Anime File");

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
	        this.setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(layout.createSequentialGroup()
	                        .addGap(61, 61, 61)
	                        .addComponent(title))
	                    .addGroup(layout.createSequentialGroup()
	                        .addGap(217, 217, 217)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
	                            .addComponent(view, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                            .addComponent(importer, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                            .addComponent(newGame, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                            .addComponent(addAnime, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
	                .addContainerGap(74, Short.MAX_VALUE))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(title)
	                .addGap(18, 18, 18)
	                .addComponent(newGame)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addComponent(addAnime)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addComponent(view)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addComponent(importer)
	                .addContainerGap(79, Short.MAX_VALUE))
	        );
	    }
	
	
	private class ButtonListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent event) 
		{
			if(event.getSource() == newGame)
			{
				panel.setAddActive(false);
				panel.setMenuActive(false);
				panel.setViewActive(false);
				panel.setGameActive(true);
				panel.update();
			}
			if(event.getSource() == view)
			{
				panel.setGameActive(false);
				panel.setAddActive(false);
				panel.setMenuActive(false);
				panel.setViewActive(true);
				panel.update();
			}
			if(event.getSource() == addAnime)
			{
				panel.setGameActive(false);
				panel.setAddActive(true);
				panel.setMenuActive(false);
				panel.setViewActive(false);
				panel.update();
			}
			if(event.getSource() == importer)
			{
				FileImporter getter = new FileImporter();
				JFileChooser fc = new JFileChooser();
				int returnVal = fc.showOpenDialog(panel);
				if (returnVal == JFileChooser.APPROVE_OPTION) 
				 {
			            File file = fc.getSelectedFile();
			            try {
							getter.importFile(file);
							JOptionPane.showMessageDialog(panel, file.getName() + " has been imported");
						} catch (FileNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				 }
				 else
				 {
					 
				 }
			}
			
		}
		
	}
}
